package com.heima.media.controller;

import com.heima.common.dto.ResponseResult;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.service.IWmSensitiveService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 敏感词信息表 前端控制器
 * </p>
 *
 * @author wsj
 * @since 2022-03-11
 */
@RestController
@RequestMapping("/api/v1/sensitive")
@Api(tags = "敏感词信息表接口")
public class WmSensitiveController{

    @Autowired
    private IWmSensitiveService wmSensitiveService;

    /**
     * 分页模糊查询敏感词列表，时间倒序
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody SensitiveDto dto) {
        return wmSensitiveService.listByPage(dto);
    }

    /**
     * 新增敏感词，不能重复
     * @param wmSensitive
     * @return
     */
    @PostMapping
    public ResponseResult add(@RequestBody WmSensitive wmSensitive) {
        return wmSensitiveService.add(wmSensitive);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable("id")Integer id) {
        wmSensitiveService.removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词，不能重复
     * @param wmSensitive
     * @return
     */
    @PutMapping
    public ResponseResult update(@RequestBody WmSensitive wmSensitive) {
        return wmSensitiveService.updateSensitve(wmSensitive);
    }

    @GetMapping("/list")
    public ResponseResult<List<String>> find() {
        List<WmSensitive> wmSensitives = wmSensitiveService.list();
        List<String> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(wmSensitives)) {
            for (WmSensitive wmSensitive : wmSensitives) {
                list.add(wmSensitive.getSensitives());
            }
        }
        return ResponseResult.okResult(list);
    }

}
